package me.zhengjie.modules.loan.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.loan.domain.Loan;
import me.zhengjie.modules.loan.service.LoanService;
import me.zhengjie.modules.loan.service.dto.LoanDto;
import me.zhengjie.modules.loan.service.dto.LoanQueryParam;
import me.zhengjie.modules.loan.service.mapper.LoanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author lida
* @date 2022-04-28
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = LoanService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LoanServiceImpl extends CommonServiceImpl<LoanMapper, Loan> implements LoanService {

    // private final RedisUtils redisUtils;
    private final LoanMapper loanMapper;

    @Override
    public PageInfo<LoanDto> queryAll(LoanQueryParam query, Pageable pageable) {
        IPage<Loan> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Loan> page = loanMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, LoanDto.class);
    }

    @Override
    public List<LoanDto> queryAll(LoanQueryParam query){
        return ConvertUtil.convertList(loanMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), LoanDto.class);
    }

    @Override
    public Loan getById(String id) {
        return loanMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public LoanDto findById(String id) {
        return ConvertUtil.convert(getById(id), LoanDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(LoanDto resources) {
        Loan entity = ConvertUtil.convert(resources, Loan.class);
        return loanMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(LoanDto resources){
        Loan entity = ConvertUtil.convert(resources, Loan.class);
        int ret = loanMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return loanMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/


//    @Override
//    public void download(List<LoanDto> all, HttpServletResponse response) throws IOException {
//      List<Map<String, Object>> list = new ArrayList<>();
//      for (LoanDto loan : all) {
//        Map<String,Object> map = new LinkedHashMap<>();
//              map.put("类型", loan.getKind());
//              map.put("对象", loan.getTarget());
//              map.put("金额", loan.getNum());
//              map.put("备注", loan.getRemarks());
//              map.put("开始时间", loan.getStartTime());
//              map.put("最终日期", loan.getFinalTime());
//                map.put(" createTime",  loan.getCreateTime());
//        list.add(map);
//      }
//      FileUtil.downloadExcel(list, response);
//    }
}
